#include <iostream>
#include <string>
#include "word_counter.h"

using namespace std;

void printUsage() {
    cout << "Usage: wc.exe [parameter] [filename]" << endl;
    cout << "Parameters:" << endl;
    cout << "  -c : Count characters" << endl;
    cout << "  -w : Count words" << endl;
    cout << "  -l : Count lines" << endl;
    cout << "  -s : Count sentences" << endl;
    cout << "  -a : Count all statistics" << endl;
    cout << "  --code : Count code lines" << endl;
    cout << "  --blank : Count blank lines" << endl;
    cout << "  --comment : Count comment lines" << endl;
}

int main(int argc, char* argv[]) {
    if (argc != 3) {
        printUsage();
        return 1;
    }

    string parameter = argv[1];
    string filename = argv[2];

    if (parameter == "-c") {
        int count = WordCounter::countCharacters(filename);
        if (count >= 0) {
            cout << "Character count: " << count << endl;
        }
        else {
            cout << "Error: Cannot open file " << filename << endl;
            return 1;
        }
    }
    else if (parameter == "-w") {
        int count = WordCounter::countWords(filename);
        if (count >= 0) {
            cout << "Word count: " << count << endl;
        }
        else {
            cout << "Error: Cannot open file " << filename << endl;
            return 1;
        }
    }
    else if (parameter == "-s") {
        int count = WordCounter::countSentences(filename);
        if (count >= 0) {
            cout << "Sentence count: " << count << endl;
        }
        else {
            cout << "Error: Cannot open file " << filename << endl;
            return 1;
        }
    }
    else if (parameter == "-l") {
        int count = WordCounter::countLines(filename);
        if (count >= 0) {
            cout << "Line count: " << count << endl;
        }
        else {
            cout << "Error: Cannot open file " << filename << endl;
            return 1;
        }
    }
    else if (parameter == "-a") {
        auto stats = WordCounter::countFileStats(filename);
        cout << "File Statistics for " << filename << ":" << endl;
        cout << "Characters: " << stats.charCount << endl;
        cout << "Words: " << stats.wordCount << endl;
        cout << "Lines: " << stats.lineCount << endl;
        cout << "Sentences: " << stats.sentenceCount << endl;
        cout << "Code Lines: " << stats.codeLines << endl;
        cout << "Blank Lines: " << stats.blankLines << endl;
        cout << "Comment Lines: " << stats.commentLines << endl;
    }
    else if (parameter == "--code") {
        int count = WordCounter::countCodeLines(filename);
        if (count >= 0) {
            cout << "Code lines: " << count << endl;
        }
        else {
            cout << "Error: Cannot open file " << filename << endl;
            return 1;
        }
    }
    else if (parameter == "--blank") {
        int count = WordCounter::countBlankLines(filename);
        if (count >= 0) {
            cout << "Blank lines: " << count << endl;
        }
        else {
            cout << "Error: Cannot open file " << filename << endl;
            return 1;
        }
    }
    else if (parameter == "--comment") {
        int count = WordCounter::countCommentLines(filename);
        if (count >= 0) {
            cout << "Comment lines: " << count << endl;
        }
        else {
            cout << "Error: Cannot open file " << filename << endl;
            return 1;
        }
    }
    else {
        cout << "Error: Unknown parameter " << parameter << endl;
        printUsage();
        return 1;
    }

    return 0;
}